from django.contrib import admin
from django.urls import reverse
from order.models import Order
from django.utils.html import format_html

# Register your models here.
@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
    list_display = ('id','model', 'cname', 'image', 'price','order_state','user_name','view_link')
    list_filter = ('order_state',)  # 添加一个基于订单状态的过滤器
    def image(self,obj):
        return format_html('<img src="/{}" alt="{}" style="width:100px;height:auto;">', obj.img, obj.cname)

    image.short_description = '实物图'
    def user_name(self,obj):
        return obj.user.name
    user_name.short_description = '用户名'
    def view_link(self, obj):
        # 'obj' 是当前的 Order 实例
        url = f'/chat/?group={obj.user.id}&user=0'
        return format_html('<a href="{}" target="_blank">联系客户</a>', url)
    view_link.short_description = '操作'



